/*
 ***************************************************************************
 * MediaTeK Inc.
 * 4F, No. 2 Technology	5th	Rd.
 * Science-based Industrial	Park
 * Hsin-chu, Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2012, MTK.
 *
 * All rights reserved.	MediaTeK's source	code is	an unpublished work	and	the
 * use of a	copyright notice does not imply	otherwise. This	source code
 * contains	confidential trade secret material of MediaTeK Tech. Any attemp
 * or participation	in deciphering,	decoding, reverse engineering or in	any
 * way altering	the	source code	is stricitly prohibited, unless	the	prior
 * written consent of MediaTeK, Inc. is obtained.
 ***************************************************************************

	Module Name:
	omci_lib_external.h

	Abstract:

	Revision History:
	Who			When			What
	--------	----------		----------------------------------------------
	Name		Date			Modification logs
	Xi.Wang		2018/9/5	Create
*/

#ifndef _OMCI_LIB_EXTERNAL_H_
#define _OMCI_LIB_EXTERNAL_H_

#include <stdint.h>
#include <pthread.h>

#ifndef GNU_PACKED
#define GNU_PACKED __attribute__ ((packed))
#endif//GNU_PACKED

#ifndef IN
#define IN
#endif
#ifndef OUT
#define OUT
#endif
#ifndef IN_OUT
#define IN_OUT
#endif

#define PRESHARED_KEY_LEN           16
#define OMCI_SN_LENS                8
#define OMCI_VERSION_LENS           14
#define OMCI_VENDOR_ID_LENS         4
#define OMCI_EQUIPMENTID_LENS       20
#define OMCI_OPERATOR_ID_LENS       4
#define OMCI_SN_MEM_LENS            16
#define OMCI_PASSWD_LENS            10
#define OMCI_LOID_LENS              24
#define OMCI_LOID_PASSWORD_LENS     12
#define OMCI_OLT_TYPE_LENS          32
#define OMCI_REG_ID_LENS            36
#define OMCI_IPHOST_MAC_LENS        6

#define STATE_UNLOCK				0
#define STATE_LOCK					1
#define OPERATIONAL_DISABLE			1
#define OPERATIONAL_ENABLE			0
#define TR69_URL_MAX_LENS			256
#define OMCI_MAX_PORTS				128
#define PORTLIST_NUM                2
#define PON_TRAP_THREAD_STOP 0
#define PON_TRAP_THREAD_START 1

#define UNIT_TYPE_MIXED_SERVICES_EQUIPMENT             45
#define UNIT_TYPE_BASET_10100                          24
#define UNIT_TYPE_WLAN                                 40
#define UNIT_TYPE_BASET_101001000                      47
#define UNIT_TYPE_VEIP                                 48
#define UNIT_TYPE_GPON24881244                         248
#define UNIT_TYPE_POTS                                 32
#define UNIT_TYPE_XGPON10G2488                         237
#define UNIT_TYPE_XGSPON10G10G                         238
#define UNIT_TYPE_VIDEO_UNI    				           38
#define UNIT_TYPE_VIDEO_ANI    				           45
#define UNIT_TYPE_ETHER_10G						       49
#define UNIT_TYPE_ETHER_2500						   50



#define BASET101001000_SLOT		    0x01	/*47	10/100/1000BaseT*/
#define BASET10100_SLOT			    0x02	/*24	10/100BaseT*/
#define POTS_SLOT 					0x04	/*32	POTS*/
#define VEIP_SLOT 					0x0a	/*48 	VEIP*/
#define WLAN_SLOT 					0x28	/*40 	WLAN*/
#define VIDEO_UNI_SLOT 				0x05    /*38 	Video UNI*/
#define VIDEO_ANI_SLOT 				0x06    /*45 	Video ANI*/
#define GPON_SLOT 					0x80	/*248 	GPON24881244*/
#define ETHER_10G_SLOT 				0x08	/*49 	10G Ethernet interface point*/
#define ETHER_2500_SLOT 			0x09	/*50 	2.5G Ethernet interface point*/


#define PPTP_POTS_MAX_NUM	64
#define VEIP_INST_MAX_NUM	8
#define TR69_INST_MAX_NUM	8

#define USQ_MODE_4Q	  4
#define USQ_MODE_8Q	  8

#define RDKB_MODULE_VENDOR_LEN		16
#define RDKB_MODULE_NAME_LEN			16
#define RDKB_MODULE_VERSION_LEN		16
#define RDKB_MODULE_FW_VERSION_LEN	16
#define RDKB_RX_LOWER_THRESH_LEN		8
#define RDKB_RX_UPPER_THRESH_LEN		8
#define RDKB_TX_LOWER_THRESH_LEN		8
#define RDKB_TX_UPPER_THRESH_LEN		8
#define RDKB_MIN_VOLTAGE_LEN			8
#define RDKB_MAX_VOLTAGE_LEN			8
#define RDKB_HIGH_BIAS_LEN				8
#define RDKB_GEM_MAX_VLAN_NUM			12
#define RDKB_INVALID_VLAN_ID			0xffff
#define RDKB_MAX_GEM_VLAN_MAP			48


typedef enum {
	SFU = 1,
	HGU = 2,
}onu_type_t;


typedef enum {
	OMCI_LINK_MODE_AUTO = 0,
	OMCI_LINK_MODE_GPON,
	OMCI_LINK_MODE_EPON,
	OMCI_LINK_MODE_10G_1G_EPON,
	OMCI_LINK_MODE_10G_10G_EPON,
	OMCI_LINK_MODE_1G_1G_EPON,
	OMCI_LINK_MODE_XGPON,
    OMCI_LINK_MODE_XGSPON,
    OMCI_LINK_MODE_NGPON2_10G_10G,
    OMCI_LINK_MODE_NGPON2_10G_2G,
    OMCI_LINK_MODE_NGPON2_2G_2G,
    OMCI_LINK_MODE_GPON_SYM,
	/*add new mode above this line!*/
	OMCI_LINK_MODE_MAX,
} onu_link_mode_t ;

typedef enum {
	RDKB_GEM_TYPE_UCAST = 0,
	RDKB_GEM_TYPE_MCAST,
	RDKB_GEM_TYPE_BCAST,
	RDKB_GEM_TYPE_MAX,
} RDKB_GEM_TYPE;

typedef enum {
	RDKB_GEM_VLAN_OP_ADD = 0,
	RDKB_GEM_VLAN_OP_REMOVE,
	RDKB_GEM_VLAN_OP_CLEAR,
	RDKB_GEM_VLAN_OP_GET,
	RDKB_GEM_VLAN_OP_MAX,
} RDKB_GEM_VLAN_OP ;

typedef struct {
	uint16_t		gemId;
	uint16_t		vlanList[RDKB_GEM_MAX_VLAN_NUM];
	RDKB_GEM_TYPE	type;
}gem_vlan_map_t;

typedef struct GNU_PACKED{
	uint8_t		domainNumber;
	uint8_t		flagField;
	uint16_t	currentUtcOffset;
	uint8_t		priority1;
	uint8_t		clockClass;
	uint8_t		accuracy;
	uint16_t	offsetScaledLogVariance;
	uint8_t		priority2;
	uint64_t	grandMasterId;
	uint16_t	stepsRemoved;
	uint8_t		timeSource;
}time_status_msg_t;

/*When omci internal creating instance, the attributes default value gets from cfg ng,
   we can extend this structure, like this:
   	struct{
		unsigned char adminState;
	} tr69MgrSvr;//CLASS ID 340
	struct{
		unsigned char validScheme;
		char username1[25];
		char password[25];
		char realm[25];
		char username2[25];
	} authSecMeth;//CLASS ID 148
******************************************************************************/
typedef struct{
	unsigned char adminState[PPTP_POTS_MAX_NUM];	
} pptpPotsUni_t;/*CLASS ID 53*/

typedef struct omciAttrDefVal_s{
	pptpPotsUni_t pptpPotsUni; /*CLASS ID 53*/
}omciAttrDefVal_t, *omciAttrDefVal_p;

typedef struct GNU_PACKED{
	uint16_t		protocol;
	uint16_t		fileType;
	uint16_t	    fileInstance;
}file_transfer_ctrl_t;

typedef struct GNU_PACKED{
	uint8_t		freqRangeLow;
	uint8_t		freqRangeHigh;
	uint8_t	    signalCapbility;
	uint8_t     opticalSignalLevel;
	uint8_t     pilotSignalLevel;
	uint8_t     signalLevelMin;
	uint8_t     signalLevelMax;
	uint32_t    pilotFreq;
}pptp_video_ani_t;

typedef struct GNU_PACKED omciOnuSlot_s
{
    uint8_t uniGE;
    uint8_t uniFE;
    uint8_t uni10GE;
	uint8_t uni2500E;	
    uint8_t veip;
    uint8_t pots;
    uint8_t videoUni;
    uint8_t videoAni;
	uint8_t wlan;
    uint8_t gpon;
} omciOnuSlot_t, *omciOnuSlot_ptr;

typedef struct GNU_PACKED{
	uint8_t		validScheme;
	char 		username1[25];	
	char 		password[25];	
	char 		realm[25];	
	char 		username2[25];	
}auth_security_meth_t;

typedef struct{
	uint16_t meId;
    uint8_t adminState;
    uint8_t operationalState;
    char interdomainName[25];
}omci_rdkb_veip_t;

typedef struct {
	uint16_t meId;
    char acsNetworkAddress[TR69_URL_MAX_LENS];
    uint16_t associatedTag;
}omci_rdkb_tr69_t;

typedef struct {
    pthread_rwlock_t rwlock;
	
	/*System capability*/
	uint8_t 	onuType;	
	char		maxPorts;
	onu_link_mode_t	linkMode;
	int 		portId[OMCI_MAX_PORTS];	
	uint32_t	eventMonitorMask;
	time_status_msg_t timeStatusMsg;
	char            usQMode;  // us queue num per tcont, 4 or 8 is valid

	/*ONU capability*/
	uint8_t 	sn[OMCI_SN_MEM_LENS];
	char 		password[OMCI_REG_ID_LENS];	
	char 		version[OMCI_VERSION_LENS];	
	uint8_t 	tmOption;
	uint8_t 	onuSurvivalTime;
	uint16_t 	extendedTcLayer;
	
	/*ONU2 capability*/
	char 		equipmentId[OMCI_EQUIPMENTID_LENS];	
	uint16_t 	vendorProCode;
	uint8_t 	omccVersion;	
	uint8_t 	securityCap;
	uint8_t  	totalTSNum;
	uint16_t 	totalPQNum;
	uint16_t	totalGEMPortNum;
	uint32_t	sysUpTime;
	uint16_t	connectivityCap;
	uint16_t	qosConfFlex;	
	
	/*ANI capability*/
	uint8_t		srIndication;
	uint16_t 	totalTcontNum;
	uint16_t	onuResponseTime;
	uint16_t	gemBlockLength;	
	
	/*ONU_E capability*/
	char	vendorId[OMCI_VENDOR_ID_LENS];
	
	/*Enhanced security control,class ID 332*/
	uint8_t enhancedPsk[PRESHARED_KEY_LEN];

	/*IPHOST Init MAC Address*/
	uint8_t iphostMac[OMCI_IPHOST_MAC_LENS];
	uint8_t iphostNum;

	file_transfer_ctrl_t fileTransCtrl;
	pptp_video_ani_t pptpVideoAni;
	auth_security_meth_t authSecurityMeth;

	int rxPowerLowerThreshold;
	int rxPowerUpperThreshold;
	int txPowerLowerThreshold;
	int txPowerUpperThreshold;

	int ponTrapThread;

	/*ONU Slot*/
	omciOnuSlot_t onuSlot;
}omci_cfg_data_t;

typedef struct {
	pthread_rwlock_t rwlock;
	int voltageMinVol;
	int voltageMaxVol;
	int biasHighBias;
	float tempMinTemp;
	float tempMaxTemp;
	int veipEntryCount;
	int tr69EntryCount;
	omci_rdkb_veip_t veipList[VEIP_INST_MAX_NUM];
	omci_rdkb_tr69_t tr69List[TR69_INST_MAX_NUM];
	gem_vlan_map_t   gemVlanMap[RDKB_MAX_GEM_VLAN_MAP];
}rdkb_omci_cfg_data_t;



int omci_lib_set_cfg_data(omci_cfg_data_t * cfg_data_ptr);
void omci_lib_notify_onu_ready(void);
void omci_lib_notify_re_config(void);

void AES_CMAC (IN uint8_t PlainText[], IN uint32_t PlainTextLength, IN uint8_t Key[],
	IN uint32_t KeyLength, OUT uint8_t MACText[], IN_OUT uint32_t *MACTextLength);

int omci_lib_get_rxPower_lowerThreshold(int * lowerThreshold);
int omci_lib_get_rxPower_upperThreshold(int * upperThreshold);
int omci_lib_get_txPower_lowerThreshold(int * lowerThreshold);
int omci_lib_get_txPower_upperThreshold(int * upperThreshold);
int omci_lib_get_pon_trap_thread(int * ponTrapThread);
int omci_lib_get_rdkb_voltage_minVol(int *minVol);
int omci_lib_get_rdkb_voltage_maxVol(int *maxVol);
int omci_lib_get_rdkb_bias_highBias(int *highBias);
int omci_lib_get_rdkb_temp_minTemp(float *minTemp);
int omci_lib_get_rdkb_temp_maxTemp(float *maxTemp);
int omci_lib_get_rdkb_veip_entry_count(int * entryNumber);
int omci_lib_get_rdkb_tr69_entry_count(int * entryNumber);
int omci_lib_get_rdkb_veip_inst_list(omci_rdkb_veip_t * veipInstList);
int omci_lib_get_rdkb_tr69_inst_list(omci_rdkb_tr69_t * tr69InstList);
int omci_lib_get_rdkb_gem_vlan(uint16_t gemId, int *type, uint16_t *vlanList,int len);
int omci_lib_set_rxPower_lowerThreshold(int lowerThreshold);
int omci_lib_set_rxPower_upperThreshold(int upperThreshold);
int omci_lib_set_txPower_lowerThreshold(int lowerThreshold);
int omci_lib_set_txPower_upperThreshold(int upperThreshold);
int omci_lib_set_pon_trap_thread(int ponTrapThread);
int omci_lib_set_rdkb_voltage_minVol(char *minVol);
int omci_lib_set_rdkb_voltage_maxVol(char *maxVol);
int omci_lib_set_rdkb_bias_highBias(char *highBias);
int omci_lib_set_rdkb_veip_entry_count(int * entryNumber);
int omci_lib_set_rdkb_tr69_entry_count(int * entryNumber);
int omci_lib_set_rdkb_veip_list(omci_rdkb_veip_t * veipInstList);
int omci_lib_set_rdkb_tr69_list(omci_rdkb_tr69_t * tr69InstList);
int omci_lib_set_rdkb_veip_adminState(uint16_t  meid, uint8_t * attrValue);
int omci_lib_set_rdkb_veip_interdomain_name(uint16_t  meid, char * attrValue);
int omci_lib_set_rdkb_tr69_url(uint16_t meid,char * buffer);
int omci_lib_set_rdkb_tr69_associatedTag(uint16_t meid, uint16_t * asscociatedTag);
int omci_lib_set_rdkb_gem_vlan(uint16_t gemId, int type, uint16_t *vlanList,int len);
int omci_lib_set_rdkb_omci_reset(void);
int omci_lib_rdkb_gem_vlan_op(uint16_t gemId, int type, uint16_t vlan,RDKB_GEM_VLAN_OP opType);

#endif
